package morenoapp.components.thinkgraphv2.components;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import morenoapp.MorenoApp;
import morenoapp.components.common.SwingAppPanel;
import morenoapp.components.common.meta.*;
import morenoapp.components.common.model.QuestrionsComboBoxModel;
import morenoapp.components.common.model.wrap.QuestionValueWrapper;
import morenoapp.components.thinkgraphv2.layout.*;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.util.DocumentedComponent;
import morenoapp.util.EnumWrapper;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;
import morenoapp.util.Order;
import org.jdesktop.application.Action;

/**
 *
 * @author dmitry.mamonov
 */
@DocumentedComponent
public class SociogramLayoutConfigPanel extends SwingAppPanel {
    //////////////////////////////////////////////
    @UseName
    JComboBox comboLayoutSelect = new JComboBox();
    //////////////////////////////////////////////
    @UseName
    JComboBox comboQuestionSelect = new JComboBox(new QuestrionsComboBoxModel());
    //////////////////////////////////////////////
    @UseName
    JButton buttonDoLayout = new JButton(getActions().get("actionDoLayout"));
    public SociogramLayoutConfigPanel() {
        super();
        initComponents();
        initLayout();
    }

    @Override
    protected void initComponents() {
        { //comboLayoutSelect
            comboLayoutSelect.setModel(new DefaultComboBoxModel(
                    EnumWrapper.createWrappers(SociogramLayoutEnum.class,
                    SociogramLayoutEnum.values())));
            comboLayoutSelect.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        AbstractSociogramLayout layout = ((EnumWrapper<SociogramLayoutEnum>) e.getItem()).getWrappedValue().getLayout();
                        //layout.doLayout(MorenoApp.getModel());
                    }
                }

            });
        }
        { //comboQuestionSelect
            //comboQuestionSelect.setModel(null);
        }
    }

    @Override
    protected void initLayout() {
               //setup content
        FormLayout layout = new FormLayout(
                //columns
                "p, 1dlu, p",
                //rows
                "p, " + //algorithm
                "p, " + //parameter
                "p" + //"do" button
                "");

        PanelBuilder builder = new PanelBuilder(layout, this);
        CellConstraints cc = new CellConstraints();

        int row = 1; //first row
        builder.addLabel(I18N.get("sociogram.select_layout_combo"), cc.rc(row, 1));
        builder.add(comboLayoutSelect, cc.rc(row, 3));
        row++;
        builder.addLabel(I18N.get("sociogram.select_question_for_layout_combo"), cc.rc(row, 1));
        builder.add(comboQuestionSelect, cc.rc(row, 3));
        row++;
        builder.add(buttonDoLayout, cc.rc(row, 3, "default, right"));
    }


    @Action
    @Order(10)
    public void actionDoLayout() {
        System.out.println("action: doLayout");
        AbstractSociogramLayout layout = ((EnumWrapper<SociogramLayoutEnum>) comboLayoutSelect.getSelectedItem()).getWrappedValue().getLayout();
        MorenoQuestionFace question = null;
        QuestionValueWrapper wrapper = (QuestionValueWrapper) comboQuestionSelect.getSelectedItem();
        if (wrapper!=null){
            question = wrapper.getValue();
        }
        if (question==null){
            JOptionPane.showMessageDialog(
                    SwingUtilities.getRoot(this), I18N.get("sociogram.question_is_not_selected_to_perform_layout"));
        } else {
            layout.doLayout(MorenoApp.getModel(), question);
        }
    }

    public static void main(String[] args) throws IOException {
        MorenoUtil.debugShowComponentInFrame(new SociogramLayoutConfigPanel());
//        MorenoTool.updateResources(new File("src"),SociogramLayoutConfigPanel.class);
    }

}
